﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.models {
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import __AS3__.vec.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class TreatmentProgressDisplayModel extends Actor {

        private var currentlyWatchedTreatment:TreatmentVO;
        private var _registeredToTickComp:Boolean;
        private var treatmentsPendingFinalizationPool:Vector.<TreatmentVO>;

        private static const POSITION_NOT_AVAILABLE:int = -1;

        public function TreatmentProgressDisplayModel(){
            this.treatmentsPendingFinalizationPool = new Vector.<TreatmentVO>();
            super();
        }
        private function stopMonitoring():void{
            var _local1:TreatmentProgressMonitoringEvent;
            if (this.isRegisteredToTickComponent){
                this._registeredToTickComp = false;
                _local1 = new TreatmentProgressMonitoringEvent(TreatmentProgressMonitoringEvent.END_MONITORING_TREATMENT);
                dispatch(_local1);
            };
        }
        private function startMonitoring(_arg1:TreatmentVO):void{
            this.updateCurrentWatchingTreatement(_arg1);
            var _local2:String = (this._registeredToTickComp) ? TreatmentProgressMonitoringEvent.UPDATE_MONITORING_TREATMENT : TreatmentProgressMonitoringEvent.START_MONITORING_TREATMENT;
            this._registeredToTickComp = true;
            var _local3:TreatmentProgressMonitoringEvent = new TreatmentProgressMonitoringEvent(_local2, this.currentlyWatchedTreatment);
            dispatch(_local3);
        }
        private function updateCurrentWatchingTreatement(_arg1:TreatmentVO):void{
            this.currentlyWatchedTreatment = _arg1;
        }
        public function currentTreatmentAvailable():Boolean{
            return (!((this.currentlyWatchedTreatment == null)));
        }
        public function get currentTreatmentMonitoring():TreatmentVO{
            return (this.currentlyWatchedTreatment);
        }
        private function get isRegisteredToTickComponent():Boolean{
            return (this._registeredToTickComp);
        }
        public function treatmentHasStarted(_arg1:TreatmentVO):void{
            if (((((_arg1) && (this.isFinishedSoonerThenCurrent(_arg1.finishedAt)))) && (this.noTreatmentsPendingFinalisation()))){
                this.stopMonitoring();
                this.startMonitoring(_arg1);
            };
        }
        private function isFinishedSoonerThenCurrent(_arg1:Number):Boolean{
            if (((this.currentTreatmentAvailable()) && (!((this.currentlyWatchedTreatment.state == GameConstants.BABY_STATE_FINALIZED))))){
                return ((this.currentlyWatchedTreatment.finishedAt >= _arg1));
            };
            return (true);
        }
        public function get getNumPendingFinalisation():int{
            return (this.treatmentsPendingFinalizationPool.length);
        }
        public function treatmentThatIsFinished(_arg1:TreatmentVO):void{
            if (this.treatmentWasSetFinishedStraightAtStartup(_arg1)){
                this.updateCurrentWatchingTreatement(_arg1);
            };
            this.addFinishedTreatmentToPendingPool(_arg1);
            dispatch(new TreatmentProgressMonitoringEvent(TreatmentProgressMonitoringEvent.START_PROCESSING_PENDING_TREATMENTS));
            this.stopMonitoring();
        }
        private function addFinishedTreatmentToPendingPool(_arg1:TreatmentVO):void{
            if (this.treatmentsPendingFinalizationPool.indexOf(_arg1) == -1){
                this.treatmentsPendingFinalizationPool.unshift(_arg1);
                this.onTreatementListUpdated();
            };
        }
        public function treatmentThatIsFinalized(_arg1:TreatmentVO):void{
            this.removeTreatmentById(_arg1.id);
            if (this.noTreatmentsPendingFinalisation()){
                this.clearCurrentWatchingTreatement();
                dispatch(new InfoEvent(EventConstants.TREATMENT_STATUS_UPDATED, EventConstants.REQUEST_FOR_FIRST_TO_FINISH_TREATMENT));
                dispatch(new TreatmentProgressMonitoringEvent(TreatmentProgressMonitoringEvent.END_PROCESSING_PENDING_TREATMENTS));
            } else {
                this.currentlyWatchedTreatment = this.getNextPendingTreatment();
            };
        }
        private function clearCurrentWatchingTreatement():void{
            this.currentlyWatchedTreatment = null;
        }
        private function getNextPendingTreatment():TreatmentVO{
            return (this.treatmentsPendingFinalizationPool[0]);
        }
        public function noTreatmentToMonitor():void{
            dispatch(new TreatmentProgressMonitoringEvent(TreatmentProgressMonitoringEvent.NO_TREATMENT_AVAILABLE));
        }
        private function noTreatmentsPendingFinalisation():Boolean{
            return ((this.treatmentsPendingFinalizationPool.length == 0));
        }
        private function removeTreatmentById(_arg1:String):void{
            var _local2:int;
            var _local3:int;
            var _local4:Boolean;
            _local2 = this.treatmentsPendingFinalizationPool.length;
            _local3 = this.positionOfFinalisedTreatment(_arg1);
            this.treatmentsPendingFinalizationPool.splice(_local3, 1);
            _local4 = !((_local2 == this.treatmentsPendingFinalizationPool.length));
            if (_local4){
                this.onTreatementListUpdated();
            };
        }
        private function positionOfFinalisedTreatment(_arg1:String):int{
            var _local4:TreatmentVO;
            var _local2:int = this.treatmentsPendingFinalizationPool.length;
            var _local3:int;
            while (_local3 < _local2) {
                _local4 = TreatmentVO(this.treatmentsPendingFinalizationPool[_local3]);
                if (_local4.id == _arg1){
                    return (_local3);
                };
                _local3++;
            };
            return (POSITION_NOT_AVAILABLE);
        }
        private function treatmentWasSetFinishedStraightAtStartup(_arg1:TreatmentVO):Boolean{
            return ((this.positionOfFinalisedTreatment(_arg1.id) == POSITION_NOT_AVAILABLE));
        }
        public function flushPendingFinalisationPool():void{
            this.treatmentsPendingFinalizationPool = new Vector.<TreatmentVO>();
            this.onTreatementListUpdated();
        }
        private function onTreatementListUpdated():void{
            var _local1:TreatmentProgressMonitoringEvent = new TreatmentProgressMonitoringEvent(TreatmentProgressMonitoringEvent.NUM_PENDING_FINALISATION_UPDATE);
            dispatch(_local1);
        }
        public function destroy():void{
            this.clearCurrentWatchingTreatement();
            this.stopMonitoring();
        }

    }
}//package net.wooga.pets.models 
